﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using Moq;

namespace BareWeb.FluentAzureDiagnostics.Tests
{
    [TestClass]
    public class DiagnosticsCaptureTest
    {


        [TestMethod]
        public void StartsWithDefaultConfiguration()
        {
            var target = new DiagnosticsCapture(CloudStorageAccount.DevelopmentStorageAccount);

            var expected = DiagnosticMonitor.GetDefaultInitialConfiguration();
            var actual = target.Default().Configuration;

            Assert.IsTrue(actual.ConfigurationChangePollInterval == expected.ConfigurationChangePollInterval);
        }

        [TestMethod]
        public void StartsWithCurrentConfiguration_Unavailable()
        {
            var target = new DiagnosticsCapture(CloudStorageAccount.DevelopmentStorageAccount);

            var expected = DiagnosticMonitor.GetDefaultInitialConfiguration();
            var actual = target.Current().Configuration;

            Assert.IsTrue(actual.ConfigurationChangePollInterval == expected.ConfigurationChangePollInterval);
        }

        [TestMethod]
        public void ChangePollInterval_Reflected()
        {
            var target = new DiagnosticsCapture(CloudStorageAccount.DevelopmentStorageAccount);

            var expected = TimeSpan.FromDays(1.66D);
            var actual = target.Default().CheckForConfigurationChangesEvery(expected).Configuration.ConfigurationChangePollInterval;

            Assert.AreEqual(expected, actual);
        }

        [TestMethod]
        public void ChangeOverallQuota_Reflected()
        {
            var target = new DiagnosticsCapture(CloudStorageAccount.DevelopmentStorageAccount);

            var expected = int.MaxValue;
            var actual = target.Default().WithOverallQuotaInMBOf(expected).Configuration.OverallQuotaInMB;

            Assert.AreEqual(expected, actual);
        
        }

        [TestMethod]
        public void ComplexTest_Valid_Directory()
        {
            Mock<IAzureBlobStorage> mockBlobStorage = new Mock<IAzureBlobStorage>();

            var azureDiagnostics = new DiagnosticsCapture(CloudStorageAccount.DevelopmentStorageAccount, mockBlobStorage.Object);
            string logPath = RoleEnvironment.IsAvailable
                                 ? RoleEnvironment.GetLocalResource("mylocallogs").RootPath
                                 : @"C:\mylogs\";

            var config = azureDiagnostics.Default()
                .WithDirectory(logPath, "bareweblogs", 1024)
                .TransferDirectoriesEvery(TimeSpan.FromHours(2D))
                .WithLogs()
                .WhereLogsAreAbove(LogLevel.Information)
                .WithPerformanceCounter(@"\Processor(*)\% Processor Time", TimeSpan.FromMinutes(1D))
                .TransferPerformanceCountersEvery(TimeSpan.FromHours(1D))
                .WithWindowsEventLog("Application!*")
                .WithWindowsEventLog("System!*")
                .TransferWindowsEventLogEvery(TimeSpan.FromHours(2D))
                .CheckForConfigurationChangesEvery(TimeSpan.FromDays(1D))
                .Configuration;

            var expected = "bareweblogs";
            var actual = config.Directories.DataSources.First().Container;

            Assert.AreEqual(expected,actual);

        }

    }
}
